<!--添加新的校准信息-->
<template>
	<view class="new-carlibration">
		<!-- <view  class="bule-button"></view> -->
		<view class="carlibration-content">
			<uni-forms ref="form" size="mini" :model="formData" :rules="rules">
				<uni-forms-item label="仪器编号" :label-width="75" name="deviceId"
					>
					<view class="" style="width: 100%!important;">
						<hpy-form-select style="padding: 0px!important;width: 100%;" v-model="formData.deviceId"
							mode="selector" title="请选择仪器编号" @change="deviceCodeSelectChange"
							:dataList="deviceList" :disabled="disableFlag"></hpy-form-select>
					</view>
				</uni-forms-item>

				<uni-forms-item label="校准人员" :label-width="75" name="examUser" 
					>
					<input v-model="formData.examUser" :maxlength="100"
						style="border:1px solid #ededed;height: 32px;border-radius: 4px;padding-left: 10px;"
						placeholder="请输入" :disabled="disableFlag" />
				</uni-forms-item>
				<uni-forms-item label="浓度" :label-width="75" name="standardValue">
					<input v-model="formData.standardValue" :maxlength="100"
						style="border:1px solid #ededed;height: 32px;border-radius: 4px;padding-left: 10px;"
						placeholder="请输入" :disabled="disableFlag" />
				</uni-forms-item>
				<uni-forms-item label="标准气" :label-width="75" name="gasType">
					<input v-model="formData.gasType" :maxlength="100"
						style="border:1px solid #ededed;height: 32px;border-radius: 4px;padding-left: 10px;"
						placeholder="请输入" :disabled="disableFlag" />
				</uni-forms-item>
				<uni-forms-item label="预热时间(min)" :label-width="75" name="warmMin">
					<view style="width: 100%!important;">
						<input v-model="formData.warmMin" :maxlength="100"
							style="border:1px solid #ededed;height: 32px;border-radius: 4px;padding-left: 10px;"
							placeholder="请输入" />
					</view>
				</uni-forms-item>
				<uni-forms-item label="流量(L/min)" :label-width="75" name="flowMin">
					<view style="width: 100%!important;">
						<input v-model="formData.flowMin" :maxlength="100"
							style="border:1px solid #ededed;height: 32px;border-radius: 4px;padding-left: 10px;"
							placeholder="请输入" />
					</view>
				</uni-forms-item>
				<uni-forms-item label="计数值" :label-width="75" name="">
					<view style="width: 100%!important;display: flex;flex-direction: row;">
						<view style="width:32%"><input v-model="formData.firstValue" :maxlength="100"
								style="border:1px solid #ededed;height: 32px;border-radius: 4px;padding-left: 10px;"
								placeholder="计数值1" /></view>
						<view style="width:32%;margin-left: 10rpx;"><input v-model="formData.secondValue" :maxlength="100"
								style="border:1px solid #ededed;height: 32px;border-radius: 4px;padding-left: 10px;"
								placeholder="计数值2" /></view>
						<view style="width:32%;margin-left: 10rpx;"><input v-model="formData.thirdValue" :maxlength="100"
								style="border:1px solid #ededed;height: 32px;border-radius: 4px;padding-left: 10px;"
								placeholder="计数值3" /></view>
					</view>
				</uni-forms-item>
				<uni-forms-item label="平均值" :label-width="75" name="averageValue">
					<view style="width: 100%!important;">
						<input v-model="averageValue" :maxlength="100"
							style="border:1px solid #ededed;height: 32px;border-radius: 4px;padding-left: 10px;" 
							placeholder="请输入" />
					</view>
				</uni-forms-item>
			</uni-forms>
			<view class="current-max">
				<view class="current-value">
					<text class="name-text">当前值：</text>
					<text class="value-text">{{averageValue}}</text>
				</view>
				<view class="max-value">
					<text class="name-text">最大值:</text>
					<text class="value-text">{{averageValue}}</text>
				</view>
			</view>
		</view>
		<view class="check-buttons">
			<button type="primary" class="mini-btn" @tap="startCalibration">开始校准</button>
			<button type="default" class="mini-btn" @tap="submitSave">保 存</button>
		</view>
	</view>
</template>
<script setup>
	import {
		onMounted,
		ref,
		computed
	} from "vue"
	import partItem from "@/components/partItem/partItem.vue";
	import {
		onLoad,
		onNavigationBarButtonTap
	} from "@dcloudio/uni-app";
	import {
		queryDeviceByFactory,deviceCorrect
	} from '@/api/deviceTest.js';
	const list = ref([])
	const texts = ref({
		title: '仪器编号',
		category: "校准人员",
		childCategory: "浓度",
		auditStatus: "标准气",
		content: "预热时间",

	})
	const subTypes = ref({})
	const types = ref({})
	const statusObj = ref({
		'-1': "待审核",
		1: "已审核",
		0: "已驳回"
	})
	const formData = ref({
		factoryId: uni.getStorageSync("factoryId"),
		taskId: "",
		examUser: "",
		gasType: "",
		standardValue: "",
		correctType: "",
		warmMin: "",
		flowMin: "",
		deviceId: "",
		firstValue: "",
		secondValue: "",
		thirdValue: "",
		averageValue: "",
		differenceValue: "",
		isPass: "",
		creatTime: ""
	})
	const currentValue = ref(20)
	const maxValue = ref(100)
	const deviceList = ref([])
	const disableFlag = ref(false)
	
	// const getClassifyFn = (parentId, isSub) => {
	// 	getClassify({
	// 		parentId
	// 	}).then(res => {
	// 		let data = res.data;
	// 		let arr = {};
	// 		data.map(v => {
	// 			arr[v.id] = v.dictValue;
	// 		})
	// 		if (isSub) {
	// 			subTypes.value = arr;
	// 			handleData(formData.value)
	// 		} else {
	// 			types.value = arr;
	// 		}
	// 	})
	// }
	//getClassifyFn('1648222707724197889');

	const submit = (auditStatus) => {
		saveInformation({
			...formData.value,
			auditStatus
		}).then(() => {
			uni.showToast("提交成功！")
			setTimeout(function() {
				uni.navigateBack()
			}, 1000)
		})
	}
	const getDeviceCode = (data) => {
		queryDeviceByFactory(data).then(res => {
			let tempData = [...res.data];
			tempData.map(v => {
				let item = {
					text: v.deviceNo,
					value: v.deviceId
				};
				deviceList.value.push(item)
			})
		})
	}
	const deviceCodeSelectChange = (e) =>{
		formData.value.deviceId = e.value.value;
	}
	// const onAverageValueInput =(e) =>{
	// 	console.log("eeeeeeee",e.value);
	// }
	const startCalibration = () =>{
		//console.log("sssssss",formData.value);
		//differenceValue（平均值-标准值）/标准值
		formData.value.differenceValue = (parseInt(averageValue.value) - parseInt(formData.value.standardValue))
	    formData.value.differenceValue = formData.value.differenceValue.toFixed(3);
		//console.log("dddddd",formData.value.differenceValue);
		//console.log("dddddd",formData.value.differenceValue.toFixed(3));
		let tempData = Math.abs(formData.value.differenceValue);
		if(tempData < 5){
		formData.value.isPass = 1;	
		} else {
		formData.value.isPass = 0;	
		}
		formData.value.averageValue = averageValue.value;
		formData.value.creatTime = getTimeNow();
		console.log("sssssss",formData.value);
	}
	const submitSave = () =>{
		//console.log("cccccccc",formData.value);
		//console.log("ccccc22222",JSON.parse(JSON.stringify(formData.value)));
		//返回列表页，修改taskid对应的那条数据
		//如果状态是通过直接保存，再返回列表页
		if(formData.value.correctType== '日常' && formData.value.isPass == 1){
			let params = JSON.parse(JSON.stringify(formData.value));
			deviceCorrect(params).then(res => {
				uni.showToast({
				icon: 'none',
				title: res.message	
				})
			})
		}
		if(formData.value.correctType== '漂移'){
			let params = JSON.parse(JSON.stringify(formData.value));
			deviceCorrect(params).then(res => {
				uni.showToast({
				icon: 'none',
				title: res.message	
				})
			})
		}
		//修改列表显示
		//通过taskId去更新列表中的数据//还要判断当前选中的哪一个tab
		let tempList = JSON.parse(JSON.stringify(uni.getStorageSync('calibrationManagementList')));
	
		let taskId = formData.value.taskId;
		tempList.forEach(item => {
			if(item.taskId == taskId){
				 Object.assign(item, formData.value)
			}
		})
	  uni.setStorageSync("calibrationManagementList",tempList)
		uni.navigateTo({
					   url: '/pages/testing/calibrationManagement'
					});
	}
	const getTimeNow = () =>{
		  var getTime = new Date().getTime(); //获取到当前时间戳
		  var time = new Date(getTime); //创建一个日期对象
		    var year = time.getFullYear(); // 年
		    var month = (time.getMonth() + 1).toString().padStart(2, '0'); // 月
		    var date = time.getDate().toString().padStart(2, '0'); // 日
		    var hour = time.getHours().toString().padStart(2, '0'); // 时
		    var minute = time.getMinutes().toString().padStart(2, '0'); // 分
		    var second = time.getSeconds().toString().padStart(2, '0'); // 秒
		    return (
		      year + "-" + month + "-" + date 
		    )
		  //+ " " + hour + ":" + minute + ":" + second
		  //console.log(time);
		  //console.log(nowDate(time));

	}
	onLoad((option) => {
		//console.log("optionoption4444",JSON.parse(option.info));
		let tempData = JSON.parse(option.info);
		formData.value.factoryId = tempData.factoryId;
		formData.value.taskId = tempData.taskId;
		formData.value.examUser = tempData.examUser;
		formData.value.gasType=tempData.gasType;
		formData.value.standardValue=tempData.standardValue;
		formData.value.correctType=tempData.correctType;
		formData.value.warmMin=tempData.warmMin;
		formData.value.flowMin=tempData.flowMin;
		formData.value.deviceId=tempData.deviceId;
		formData.value.firstValue=tempData.firstValue;
		formData.value.secondValue=tempData.secondValue;
		formData.value.thirdValue=tempData.thirdValue;
		averageValue.value=tempData.averageValue;
		formData.value.differenceValue=tempData.differenceValue;
		if(tempData.taskId == '1' || tempData.taskId == '4'){
			disableFlag.value = true;
		}
		
	});
	onNavigationBarButtonTap(function(e) {
		//console.log("eeeee", e.fontSize);
	})
	onMounted(() => {
		let factoryId = uni.getStorageSync("factoryId");
		let params = {
			factoryId: factoryId
		};
		//获取仪器编号
		getDeviceCode(params);
	})
	const averageValue = computed(() => {
		 return (parseInt(formData.value.firstValue) + parseInt(formData.value.secondValue) +parseInt(formData.value.thirdValue))/3
	})
</script>

<style lang="scss" scoped>
	.item {
		display: flex;
		width: 100%;
		max-width: 100%;
		padding-top: 10rpx !important;
	}

	.new-carlibration {
		background: #fff;
		margin: 30rpx;
		padding: 20rpx;
		border-radius: 10rpx;

		// .bule-button{
		// 	position: absolute;
		// 	right:20rpx;
		// 	width:60rpx;
		// 	height:60rpx;
		// 	background-image:url('@/static/deviceImg/bule-icon.png') ;
		// }
		.carlibration-content {}
	}

	.current-max {
		width: 100%;
		height: 100rpx;
		background-color: #00aaff;
		display: flex;
		flex-direction: row;

		.current-value {
			width: 45%;
			text-align: center;
			line-height: 100rpx;
		}

		.max-value {
			width: 45%;
			margin-left: 10rpx;
			text-align: center;
			line-height: 100rpx;
		}

		.name-text {
			color: #ffffff;
		}

		.value-text {
			font-size: 24px;
			color: #ffffff;
		}
	}

	.check-buttons {
		display: flex;
		padding: 20rpx 0;
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		background: rgba(255, 255, 255, 0.6);

		uni-button {
			width: 40%;
			font-size: 26rpx;
			line-height: 80rpx !important;
		}
	}
</style>